package cn.itsource.basic.controller;

import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.FastDfsApiOpr;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("/fastDfs")
public class FastDFSController {


    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file){
        //1.如果能够拿到上传的文件--file
        try {
            //获取文件名字
            String filename = file.getOriginalFilename();//addsao.jpg
            //获取文件的后缀
            /*
                <dependency>
                    <groupId>commons-fileupload</groupId>
                    <artifactId>commons-fileupload</artifactId>
                    <version>1.3.1</version>
                </dependency>
             */
            String ext = FilenameUtils.getExtension(filename);
            //文件上传 获取文件的路径
            String uploadPath = FastDfsApiOpr.upload(file.getBytes(), ext);
            //把上传的路径返回给前端页面
            return  AjaxResult.me().setResultObj(uploadPath);
        } catch (IOException e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }

}
